home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / scripts / mysql_secure_installation < prev    next >
Encoding:
Text File  |  2004-10-28  |  6.2 KB  |  313 lines

  1. #!/bin/sh
  2.  
  3. # Copyright (C) 2002 MySQL AB and Jeremy Cole
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  15.  
  16. config=".my.cnf.$$"
  17. command=".mysql.$$"
  18.  
  19. trap "interrupt" 2
  20.  
  21. rootpass=""
  22.  
  23. prepare() {
  24.     touch $config $command
  25.     chmod 600 $config $command
  26. }
  27.  
  28. do_query() {
  29.     echo $1 >$command
  30.     mysql --defaults-file=$config <$command
  31.     return $?
  32. }
  33.  
  34. make_config() {
  35.     echo "# mysql_secure_installation config file" >$config
  36.     echo "[mysql]" >>$config
  37.     echo "user=root" >>$config
  38.     echo "password=$rootpass" >>$config
  39. }
  40.  
  41. get_root_password() {
  42.     status=1
  43.     while [ $status -eq 1 ]; do
  44.     stty -echo
  45.     echo -n "Enter current password for root (enter for none): "
  46.     read password
  47.     echo
  48.     stty echo
  49.     if [ "x$password" = "x" ]; then
  50.         hadpass=0
  51.     else
  52.         hadpass=1
  53.     fi
  54.     rootpass=$password
  55.     make_config
  56.     do_query ""
  57.     status=$?
  58.     done
  59.     echo "OK, successfully used password, moving on..."
  60.     echo
  61. }
  62.  
  63. set_root_password() {
  64.     stty -echo
  65.     echo -n "New password: "
  66.     read password1
  67.     echo
  68.     echo -n "Re-enter new password: "
  69.     read password2
  70.     echo
  71.     stty echo
  72.  
  73.     if [ "$password1" != "$password2" ]; then
  74.     echo "Sorry, passwords do not match."
  75.     echo
  76.     return 1
  77.     fi
  78.  
  79.     if [ "$password1" = "" ]; then
  80.     echo "Sorry, you can't use an empty password here."
  81.     echo
  82.     return 1
  83.     fi
  84.  
  85.     do_query "UPDATE mysql.user SET Password=PASSWORD('$password1') WHERE User='root';"
  86.     if [ $? -eq 0 ]; then
  87.     echo "Password updated successfully!"
  88.     echo "Reloading privilege tables.."
  89.     if ! reload_privilege_tables; then
  90.         exit 1
  91.     fi
  92.     echo
  93.     rootpass=$password1
  94.     make_config
  95.     else
  96.     echo "Password update failed!"
  97.     exit 1
  98.     fi
  99.  
  100.     return 0
  101. }
  102.  
  103. remove_anonymous_users() {
  104.     do_query "DELETE FROM mysql.user WHERE User='';"
  105.     if [ $? -eq 0 ]; then
  106.     echo " ... Success!"
  107.     else
  108.     echo " ... Failed!"
  109.     exit 1
  110.     fi
  111.  
  112.     return 0
  113. }
  114.  
  115. remove_remote_root() {
  116.     do_query "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';"
  117.     if [ $? -eq 0 ]; then
  118.     echo " ... Success!"
  119.     else
  120.     echo " ... Failed!"
  121.     fi
  122. }
  123.  
  124. remove_test_database() {
  125.     echo " - Dropping test database..."
  126.     do_query "DROP DATABASE test;"
  127.     if [ $? -eq 0 ]; then
  128.     echo " ... Success!"
  129.     else
  130.     echo " ... Failed!  Not critical, keep moving..."
  131.     fi
  132.  
  133.     echo " - Removing privileges on test database..."
  134.     do_query "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
  135.     if [ $? -eq 0 ]; then
  136.     echo " ... Success!"
  137.     else
  138.     echo " ... Failed!  Not critical, keep moving..."
  139.     fi
  140.  
  141.     return 0
  142. }
  143.  
  144. reload_privilege_tables() {
  145.     do_query "FLUSH PRIVILEGES;"
  146.     if [ $? -eq 0 ]; then
  147.     echo " ... Success!"
  148.     return 0
  149.     else
  150.     echo " ... Failed!"
  151.     return 1
  152.     fi
  153. }
  154.  
  155. interrupt() {
  156.     echo
  157.     echo "Aborting!"
  158.     echo
  159.     cleanup
  160.     stty echo
  161.     exit 1
  162. }
  163.  
  164. cleanup() {
  165.     echo "Cleaning up..."
  166.     rm -f $config $command
  167. }
  168.  
  169.  
  170. # The actual script starts here
  171.  
  172. prepare
  173.  
  174. echo
  175. echo
  176. echo
  177. echo
  178. echo "NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL"
  179. echo "      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!"
  180. echo
  181. echo
  182.  
  183. echo "In order to log into MySQL to secure it, we'll need the current"
  184. echo "password for the root user.  If you've just installed MySQL, and"
  185. echo "you haven't set the root password yet, the password will be blank,"
  186. echo "so you should just press enter here."
  187. echo
  188.  
  189. get_root_password
  190.  
  191.  
  192. #
  193. # Set the root password
  194. #
  195.  
  196. echo "Setting the root password ensures that nobody can log into the MySQL"
  197. echo "root user without the proper authorisation."
  198. echo
  199.  
  200. if [ $hadpass -eq 0 ]; then
  201.     echo -n "Set root password? [Y/n] "
  202. else
  203.     echo "You already have a root password set, so you can safely answer 'n'."
  204.     echo
  205.     echo -n "Change the root password? [Y/n] "
  206. fi
  207.  
  208. read reply
  209. if [ "$reply" = "n" ]; then
  210.     echo " ... skipping."
  211. else
  212.     status=1
  213.     while [ $status -eq 1 ]; do
  214.     set_root_password
  215.     status=$?
  216.     done
  217. fi
  218. echo
  219.  
  220.  
  221. #
  222. # Remove anonymous users
  223. #
  224.  
  225. echo "By default, a MySQL installation has an anonymous user, allowing anyone"
  226. echo "to log into MySQL without having to have a user account created for"
  227. echo "them.  This is intended only for testing, and to make the installation"
  228. echo "go a bit smoother.  You should remove them before moving into a"
  229. echo "production environment."
  230. echo
  231.  
  232. echo -n "Remove anonymous users? [Y/n] "
  233.  
  234. read reply
  235. if [ "$reply" = "n" ]; then
  236.     echo " ... skipping."
  237. else
  238.     remove_anonymous_users
  239. fi
  240. echo
  241.  
  242.  
  243. #
  244. # Disallow remote root login
  245. #
  246.  
  247. echo "Normally, root should only be allowed to connect from 'localhost'.  This"
  248. echo "ensures that someone cannot guess at the root password from the network."
  249. echo
  250.  
  251. echo -n "Disallow root login remotely? [Y/n] "
  252. read reply
  253. if [ "$reply" = "n" ]; then
  254.     echo " ... skipping."
  255. else
  256.     remove_remote_root
  257. fi
  258. echo
  259.  
  260.  
  261. #
  262. # Remove test database
  263. #
  264.  
  265. echo "By default, MySQL comes with a database named 'test' that anyone can"
  266. echo "access.  This is also intended only for testing, and should be removed"
  267. echo "before moving into a production environment."
  268. echo
  269.  
  270. echo -n "Remove test database and access to it? [Y/n] "
  271. read reply
  272. if [ "$reply" = "n" ]; then
  273.     echo " ... skipping."
  274. else
  275.     remove_test_database
  276. fi
  277. echo
  278.  
  279.  
  280. #
  281. # Reload privilege tables
  282. #
  283.  
  284. echo "Reloading the privilege tables will ensure that all changes made so far"
  285. echo "will take effect immediately."
  286. echo
  287.  
  288. echo -n "Reload privilege tables now? [Y/n] "
  289. read reply
  290. if [ "$reply" = "n" ]; then
  291.     echo " ... skipping."
  292. else
  293.     reload_privilege_tables
  294. fi
  295. echo
  296.  
  297. cleanup
  298.  
  299. echo
  300. echo
  301. echo
  302. echo "All done!  If you've completed all of the above steps, your MySQL"
  303. echo "installation should now be secure."
  304. echo
  305. echo "Thanks for using MySQL!"
  306. echo
  307. echo
  308.  
  309.  
  310.